<?php

namespace App\Http\Controllers\Alipay;

use App\Http\Controllers\Alipay\BaseController;
use App\Models\AuthenticLog;
use App\Models\Config;
use App\Models\MemberOther;
use App\Models\Order;
use App\Services\CommonService;
use App\Services\EsignV3Service;
use App\Services\IdCardService;
use App\Services\XinyanService;
use Illuminate\Http\Request;

class AuthenticNewController extends BaseController
{
    public function realPersonAuth1(Request $request)
    {
        $certName = $request->input('certName');
        $certNo = $request->input('certNo') ? $request->input('certNo') : '';

        $type = $request->input('type') ? $request->input('type') : '1';

        if (!$certName || !$certNo) {
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '参数错误');
        }
        if (!check_passportid($certNo)) {
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '身份证号无效');
        }
        $member=$this->member;

        $age = getAgeByID($certNo);
        if ($age) {
            $config = Config::settingDetail();
            if ($age < $config['min_age'] || $age > $config['max_age']) {
                return ReturnMsg(CommonService::HTTP_CODE_FAIL, '您年龄不符合要求');
            }
        }

        if (empty($member['mobile'])) {
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '未授权手机号码');
        }

        if ($this->member['cert_no'] && $this->member['cert_name']) {
            return   ReturnMsg(CommonService::HTTP_CODE_FAIL, '已实名认证');
        }
          if (mb_strlen($certName)>5) {
            return   ReturnMsg(CommonService::HTTP_CODE_FAIL, '参数格式有误');
        }

//        XinyanService::threeElementsFace(
//            [
//                'uid'=>$member['uid'],
//                'cert_name'=>$certName,
//                'cert_no'=>$certNo,
//                'mobile'=>$member['mobile']
//            ]
//        );

        $response = EsignV3Service::checkFaceAuth("", $certName, $certNo, $type);//e签宝实人认证
        if ($response) {

            $authenticLogId = AuthenticLog::where(['cert_no' => $certNo,  'uid' => $member['uid'],'status'=>0])->orderBy('id', 'desc')->value('id');
            //更改实名认证
            $resData = $response;
            $data = [
                'uid' => $member['id'],
                'cert_no' => $certNo,
                'cert_name' => $certName,
                //更改实名认证
                'certify_id' => $resData['flowId'],
                'request' => json_encode($response),
            ];

            if ($authenticLogId) {
                AuthenticLog::where(['id' =>$authenticLogId])->update($data);
            } else {
                AuthenticLog::insert($data);
            }
            return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '唤起实名认证成功', [
                //更改实名认证
                "faceToken" => $resData['faceToken'],
                "certify_id" => $resData['flowId'],
                "certify_url" => $resData['originalUrl']
            ]);
        }
        return ReturnMsg(CommonService::HTTP_CODE_FAIL, '唤起实名认证失败');


    }
}
